home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / LogoMation 1.0.1 / Examples / Lib / Hanoi Towers.lib / Hanoi Towers.lib
Encoding:
Text File  |  1993-03-03  |  3.0 KB  |  117 lines  |  [UVtx/UVtl]

  1. // library routines for the Towers of Hanoi
  2.  
  3. // GetNumberOfTowers - ask for the number, and return it
  4. ////////////////////////////////////////////////////
  5. Function GetNumberOfTowers(n)
  6.     Pen name="GNTpen"
  7.     Clear 53066,64909,65535
  8.     Color 56912,2891,21210
  9.     Up
  10.     Goto -100,0,0
  11.     Print "Number of rings (1..10): ", move
  12.     Down
  13.     Forward 25
  14.     Backward 25
  15.     n = ask(30)
  16.     If (n < 1) | (n > 10)
  17.         Up
  18.         Goto -100,-30,0
  19.         Print "Sorry, must be between 1 and 10!"
  20.         Sound "Oops"
  21.         Halt
  22.     Return n
  23.     
  24. // SetUp - build the towers, build the ring, init vars
  25. ///////////////////////////////////////////////
  26. Function SetUp()
  27.     ringHeight = 15
  28.     // create the N rings
  29.     Repeat i,1,N
  30.         Picture "ring" . i
  31.             Up
  32.             Goto -i*5,0,0
  33.             Down
  34.             Fill 1,random(65000),random(65000),random(65000)
  35.                 Repeat 2
  36.                     Forward i*10
  37.                     Left 90
  38.                     Forward ringHeight-1
  39.                     Left 90
  40.         Pen name=i, picture="ring" . i   // pen i is ring i
  41.         Pen name=""
  42.     Clear
  43.     // a pen for writing the number of rings moved
  44.     nMoved = 0
  45.     Pen name="moved", fontsize=20,fontstyle="Bold",fontname="Times"
  46.     Up
  47.     Goto -width()/2+100, height()/2-40,0
  48.     Color 6360,65535,4549
  49.     Print "moving " . (2^N - 1) . " rings"
  50.     Goto width()/2-100, height()/2-40,0
  51.     Pen name=""
  52.     // draw the towers
  53.     towerBaseY = -height()/2 + 20
  54.     towerTopY = towerBaseY + ringHeight * 11
  55.     Repeat i,1,3
  56.         towerX[i] = (i-2) * 110
  57.         Up
  58.         Goto towerX[i]-3, towerBaseY, 0
  59.         Down
  60.         Fill 1,0,0,0
  61.             Repeat 2
  62.                 Forward 6
  63.                 Left 90
  64.                 Forward towerTopY-towerBaseY
  65.                 Left 90
  66.                 
  67.     // drop the N ring on tower #1
  68.     nTower[1] = N
  69.     nTower[2] = 0
  70.     nTower[3] = 0
  71.     Repeat i,N,1,-1
  72.         Pen name=i, speed=1000,end="yes"
  73.         Up
  74.         Goto towerX[1], height(),0
  75.         Down
  76.         Sound "Whip",wait
  77.         Goto x(), towerBaseY + (N-i)*ringHeight,-90
  78.         dong(5)
  79.     
  80.     
  81. // MoveOne - move one ring from one pole to another
  82. ///////////////////////////////////////////////
  83. Function MoveOne(ring,from,to)
  84.     If doPause
  85.         ask(1)
  86.     Pen name=ring, speed=speed1
  87.     Down
  88.     Goto towerX[from], towerTopY+50, 0
  89.     Goto towerX[to], y(), 0
  90.     Pause thePause
  91.     Pen speed=speed2
  92.     Goto x(), towerBaseY + nTower[to]*ringHeight,-90
  93.     dong(3)
  94.     nTower[from] = nTower[from] - 1
  95.     nTower[to] = nTower[to] + 1
  96.     Pen name="moved"
  97.     Down
  98.     Color 65535,65535,65535
  99.     Fill 1,65535,65535,65535
  100.         Repeat 2
  101.             Forward 99
  102.             Left 90
  103.             Forward 39
  104.             Left 90
  105.     Color 65535,7106,29616
  106.     nMoved = nMoved+1
  107.     Print nMoved
  108.  
  109. // dong - bounce the disk and play the metal sound
  110. /////////////////////////////////////////////
  111. Function dong(n,a)
  112.     Sound "Pole", nowait
  113.     Pen speed=200
  114.     Repeat a,n,1,-1
  115.         Backward a
  116.         Forward a
  117.